<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>=^.^=</title>
<link rel="stylesheet" href="./style.css">
<style>
canvas {
	cursor: grab;
}
canvas.-dragging {
	cursor: grabbing;
}
canvas.-moving {
	cursor: move;
}
canvas.-zoomIn {
	cursor: zoom-in;
}
</style>
</head>
<body>
<div class="info">
	<p><a href="https://github.com/yomotsu/camera-controls">GitHub repo</a></p>
	<label><input type="checkbox" onchange="cameraControls.verticalDragToForward = this.checked">vertical drag to move forward</label>
	<br>
	<label><input type="checkbox" onchange="cameraControls.dollyToCursor = this.checked">dolly to cursor</label>
</div>

<script src="https://unpkg.com/three@0.118.3/build/three.min.js"></script>
<script src="../dist/camera-controls.js"></script>
<script>
CameraControls.install( { THREE: THREE } );

const width  = window.innerWidth;
const height = window.innerHeight;
const clock = new THREE.Clock();
const scene  = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 60, width / height, 0.01, 100 );
camera.position.set( 0, 0, 5 );
const renderer = new THREE.WebGLRenderer();
renderer.setSize( width, height );
document.body.appendChild( renderer.domElement );

const cameraControls = new CameraControls( camera, renderer.domElement );
cameraControls.addEventListener( 'controlstart', function() {

	switch ( cameraControls.currentAction ) {
		case CameraControls.ACTION.ROTATE:
		case CameraControls.ACTION.TOUCH_ROTATE: {

			renderer.domElement.classList.add( '-dragging' );
			break;

		}

		case CameraControls.ACTION.TRUCK:
		case CameraControls.ACTION.TOUCH_TRUCK: {

			renderer.domElement.classList.add( '-moving' );
			break;

		}

		case CameraControls.ACTION.DOLLY:
		case CameraControls.ACTION.ZOOM: {

			renderer.domElement.classList.add( '-zoomIn' );
			break;

		}

		case CameraControls.ACTION.TOUCH_DOLLY_TRUCK:
		case CameraControls.ACTION.TOUCH_ZOOM_TRUCK: {

			renderer.domElement.classList.add( '-moving' );
			break;

		}

		default: {
			break;
		}
	}

} );
cameraControls.addEventListener( 'controlend', function() {

	renderer.domElement.classList.remove(
		'-dragging',
		'-moving',
		'-zoomIn'
	);

} );

const mesh = new THREE.Mesh(
	new THREE.BoxGeometry( 1, 1, 1 ),
	new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } )
);
scene.add( mesh );

const gridHelper = new THREE.GridHelper( 50, 50 );
gridHelper.position.y = - 1;
scene.add( gridHelper );

renderer.render( scene, camera );

( function anim () {

	const delta = clock.getDelta();
	const elapsed = clock.getElapsedTime();
	const updated = cameraControls.update( delta );

	// if ( elapsed > 30 ) { return; }

	requestAnimationFrame( anim );

	if ( updated ) {

		renderer.render( scene, camera );
		console.log( 'rendered' );

	}

} )();
</script>

</body>
</html>
